#define _CRT_SECURE_NO_WARNINGS 1
#include "stack.h"
void STInit(ST* p)
{
	assert(p);
	ST* cur = p;
	cur->capacity = cur->top = 0;
	cur->a = NULL;
}
void STPush(ST* p, STDataType x)
{
	assert(p);
	ST* cur = p;
	if (cur->capacity == cur->top)
	{
		int newnode = cur->capacity == 0 ? 4 : 2 * cur->capacity;
		STDataType* tmp = (STDataType*)realloc(p->a,sizeof(STDataType)*newnode);
		if (tmp == NULL)
		{
			perror("realloc error");
			exit(-1);
		}
		cur->capacity = newnode;
		cur->a = tmp;
	}
	cur->a[cur->top] = x;
	cur->top++;
}

STDataType STTop(ST* p)
{
	assert(p);
	assert(p->top >0);
	return p->a[p->top-1];
}

void STDestroy(ST* p)
{
	assert(p);
	ST* cur = p;
	free(cur->a);
	cur->capacity = cur->top = 0;
}

void STPop(ST* p)
{
	assert(p);
	assert(p->top>0);
	--p->top;
}

int STSize(ST* p)
{
	assert(p);
	return p->top;
}

bool STEmpty(ST* p)
{
	return p->top == 0;
}